//
//  SQDetailsModifyViewController.m
//  energy
//
//  Created by 周曼迪 on 2021/7/12.
//

#import "SQDetailsModifyViewController.h"

#import "SQHierarchyModel.h"
#import "SQHiddenDangerModel.h"
#import "SQQueryModel.h"

#import "PickerSelectView.h"
#import "LCImageChoose.h"
#import "SQShowImage.h"

#import "SQVoiceView.h"
#import "NSString+NCDate.h"



@interface SQDetailsModifyViewController ()<BaseTableVCInterface>


/**录音*/
@property (strong, nonatomic) SQVoiceView *voiceView;
/**语音view*/
@property (weak, nonatomic) IBOutlet UIView *view_voice;

@property (strong, nonatomic) SQQueryModel *queryModel;
@property (weak, nonatomic) IBOutlet UITextField *tf_area;//区域
@property (weak, nonatomic) IBOutlet UITextField *tf_hierarchy;//层级
@property (weak, nonatomic) IBOutlet UITextField *tf_people;//责任人
@property (strong, nonatomic) NSMutableArray *peopleArrayM;
@property (weak, nonatomic) IBOutlet UITextView *tv_content;
@property (strong, nonatomic) SQHierarchyModel *hierarchyModel;

@property (weak, nonatomic) IBOutlet UIImageView *img_picOne;
@property (weak, nonatomic) IBOutlet UIImageView *img_picOneDel;
@property (weak, nonatomic) IBOutlet UIImageView *img_picTwo;
@property (weak, nonatomic) IBOutlet UIImageView *img_picTwoDel;
@property (weak, nonatomic) IBOutlet UIImageView *img_picThree;
@property (weak, nonatomic) IBOutlet UIImageView *img_picThreeDel;


/**上传图片*/
@property (strong, nonatomic) NSString *hd_report_one;
@property (strong, nonatomic) NSString *hd_report_two;
@property (strong, nonatomic) NSString *hd_report_three;
/**上传音频*/
@property (strong, nonatomic) NSString *hd_report_audio;

@property (copy, nonatomic) NSString *department_id;
@property (copy, nonatomic) NSString *department;
@property (copy, nonatomic) NSString *department_name;

@property (copy, nonatomic) NSString *person;
@property (copy, nonatomic) NSString *person_id;
@property (copy, nonatomic) NSString *person_name;


@end

@implementation SQDetailsModifyViewController

- (void)viewDidLoad {
    self.tableViewInterface = self;
    [super viewDidLoad];
    self.navigationItem.title = @"隐患详情";
    self.department = @"1";
    self.person = @"1";
    
    self.hd_report_one = @"";
    self.hd_report_two = @"";
    self.hd_report_three = @"";
    self.hd_report_audio = @"";
    

    
    self.voiceView = [[SQVoiceView alloc]init];
    [self.voiceView showWithChooseBlock:^(NSString * _Nonnull path) {
        self.hd_report_audio = path;

    }];
    [self.view_voice  addSubview:self.voiceView];
    
    [self getHierarchy];
    
    [self getQueryData];

}

-(void)getQueryData{
    [SQQueryModel getDataWithParams:@{@"hd_id":self.hd_id} WithCallBack:^(SQQueryModel * data) {
        self.queryModel = data;
        [self getHierarchy];
        self.tf_area.text = data.data.site_area;
        self.tv_content.text = data.data.hd_danger_description;
        
        [self.img_picOne setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",LC_ReqHeader,data.data.hd_report_pic_one]] placeholder:[UIImage imageNamed:@"addImage"]];
        if (![data.data.hd_report_pic_one isEqualToString:@""]) {
            self.img_picOneDel.image = [UIImage imageNamed:@"del"];
        }
        
        
        [self.img_picTwo setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",LC_ReqHeader,data.data.hd_report_pic_two]] placeholder:[UIImage imageNamed:@"addImage"]];
        if (![data.data.hd_report_pic_two isEqualToString:@""]) {
            self.img_picTwoDel.image = [UIImage imageNamed:@"del"];
        }
        
        
        [self.img_picThree setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",LC_ReqHeader,data.data.hd_report_pic_three]] placeholder:[UIImage imageNamed:@"addImage"]];
        if (![data.data.hd_report_pic_three isEqualToString:@""]) {
            self.img_picThreeDel.image = [UIImage imageNamed:@"del"];
        }
        
        [self.voiceView buildPlayVoiceVie: data.data.hd_report_pic_audio];
        
        self.hd_report_one =  data.data.hd_report_pic_one;
        self.hd_report_two =  data.data.hd_report_pic_two;
        self.hd_report_three = data.data.hd_report_pic_three;
        self.hd_report_audio = data.data.hd_report_pic_audio;
        
       
    }];
}


-(void)getHierarchy{
    NSString * os_level;
        if ([MLevel isEqualToString:@"1"] ) {
            os_level = MOne;
        }else if ( [MLevel isEqualToString:@"2"] || [MLevel isEqualToString:@"3"]){
            os_level = MTwo;
        }else if ( [MLevel isEqualToString:@"4"] ){
            os_level = MThree;
        }
    
    
    [SQHierarchyModel getDataWithParams:@{@"m_enterprise_id":MEnterpriseId,@"m_level":MLevel,@"os_level":os_level} WithCallBack:^(SQHierarchyModel * data) {
        self.hierarchyModel = data;
        
        for (int i = 0; i < data.data.count; i ++) {
            SQHierarchyModelArray * dataModel = data.data[i];
            if ([self.queryModel.data.hd_confirm_department_id isEqualToString:dataModel.os_id]) {
                self.tf_hierarchy.text = dataModel.os_name;
                self.department_id = dataModel.os_id;
                self.department_name = dataModel.os_name;
                for (int f = 0; f < dataModel.personnel.count; f ++) {
                    SQHierarchyModelPersonnelArray * personnelModel = dataModel.personnel[f];
                    if ([personnelModel.m_id isEqualToString:self.queryModel.data.hd_confirm_person_id]) {
                        self.tf_people.text = personnelModel.m_name;
                        self.person_name = personnelModel.m_name;
                        self.person_id = personnelModel.m_id;
                    }
                }
                
            }
            
        }
    }];
}


-(NSInteger)rowsCount:(NSInteger)section{
    return 0;
}


-(NSArray<NSArray<NSString *> *> *)classArr{
    return @[@[@"SQHiddenDangerTableViewCell"]];
}

//༥༤༽ཀབཀྲ་ཤིས་བདེ་ལེགས།

/**隐患确认层级*/
- (IBAction)clickHierarchy:(UIButton *)sender {
    [self.view endEditing:YES];
    
    if (self.hierarchyModel == nil) {return;}
    
    self.hierarchyModel.nameArr.pickerSigleShow(self.tf_hierarchy,@"隐患确认层级").finish = ^(UITextField * textField){
        self.department = @"2";
        SQHierarchyModelArray * data = self.hierarchyModel.data[self.tf_hierarchy.selectIndex[0].intValue];
        self.department_id = data.os_id;
        self.department_name = data.os_name;
    };;

}



/**隐婚确认负责人*/
- (IBAction)clickPeople:(UIButton *)sender {
    [self.view endEditing:YES];
    if ([self.department isEqualToString:@"1"]) {
        [CCToast showMessage:@"请选择隐患确认层级"];
        return;
    }

    self.hierarchyModel.data[self.tf_hierarchy.selectIndex[0].intValue].nameArr.pickerSigleShow(self.tf_people,@"隐患确认负责人").finish = ^(UITextField * textField){
        SQHierarchyModelArray * data = self.hierarchyModel.data[self.tf_hierarchy.selectIndex[0].intValue];
        SQHierarchyModelPersonnelArray * personnelData = data.personnel[self.tf_people.selectIndex[0].intValue];
        self.person_id = personnelData.m_id;
        self.person_name = personnelData.m_name;
        SQ_LOG(@"%@",personnelData.m_id);
    };

}

/**确认*/
- (IBAction)clickConfirm:(UIButton *)sender {
 
    if ([self verification]) {
        [SQHiddenDangerModel getDataWithParams:@{@"hd_report_pic_one":self.hd_report_one,
                                                 @"hd_report_pic_two":self.hd_report_two,
                                                 @"hd_report_pic_three":self.hd_report_three,
                                                 @"hd_id":self.hd_id,
                                                 @"hd_ipm_one":@"",
                                                 @"hd_ipm_two":@"",
                                                 @"hd_ipm_three":@"",
                                                 @"hd_ipm":@"0",
                                                 @"hd_ipm_text":@"",
                                                 @"hd_rt":@"0",
                                                 @"hd_rt_text":@"",
                                                 @"hd_rp":@"0",
                                                 @"hd_rp_text":@"",
                                                 @"hd_risktype":@"4",
                                                 @"site_area":self.tf_area.text,
                                                 @"hd_type":@"1",
                                                 @"m_level":MLevel,
                                                 @"hd_report_person_id":UID,
                                                 @"hd_report_person_text":MName,
                                                 @"hd_report_department_id":MGroup,
                                                 @"hd_report_department_text":MNumber,
                                                 @"hd_confirm_department_id":self.department_id,
                                                 @"hd_confirm_person_id":self.person_id,
                                                 @"hd_confirm_person_text":self.person_name,
                                                @"hd_report_pic_audio":self.hd_report_audio,
                                                 @"hd_danger_description":self.tv_content.text?self.tv_content.text:@"",
                                                 @"hd_enterprise":MEnterpriseId,
                                                 @"hd_enterprise_text":MEnterpriseText

        } WithCallBack:^(SQHiddenDangerModel * data) {
            [CCToast showMessage:@"提交成功"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"list" object:nil];
            [self.navigationController popViewControllerAnimated:YES];
        }];
    }
}



#pragma mark ---- 验证
- (BOOL)verification {
    BOOL ret = NO;
    if([self.tf_area.text isEqualToString:@""]){
       
        [CCToast showMessage:@"请输入所在地区"];
        return NO;
    }
    
    
    if([self.tf_hierarchy.text isEqualToString:@""]){
       
        [CCToast showMessage:@"请选择层级"];
        return NO;
    }
    if([self.tf_people.text isEqualToString:@""]){
       
        [CCToast showMessage:@"请选择责任人"];
        return NO;
    }
    if([self.tv_content.text isEqualToString:@""]){
       
        [CCToast showMessage:@"请填写隐患描述"];
        return NO;
    }if (![self haveStr:@[self.hd_report_one,self.hd_report_two,self.hd_report_three,self.hd_report_audio]]) {
            
            [CCToast showMessage:@"请上传照片或者音频"];
            return NO ;
        
    } else{
        ret = YES;
    }
    return YES;
}


- (BOOL)haveStr:(NSArray<NSString *> *)strs{
    for (NSString *key in strs) {
        if (key.length > 0) {
            return YES;
        }
    }
    return NO;
}


- (IBAction)clickPicOne:(id)sender {
    [self.view endEditing:YES];
    
    if ([self haveStr:@[self.hd_report_one]]) {
        [SQShowImage showImage:self.img_picOne];
        
    }else{
        [LCImageChoose shareImageWithKey:@"" withImageMax:1 withCallBack:^(NSArray *data) {
            self.img_picOne.image = data[0];
            self.hd_report_one =   [UIImageJPEGRepresentation(self.img_picOne.image, 0.5f) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
          
            self.img_picOneDel.image = [UIImage imageNamed:@"del"];
        }];
    }
    
   
}

- (IBAction)clickDelPicOne:(id)sender {
    self.img_picOne.image = [UIImage imageNamed:@"addImage"];
   self.img_picOneDel.image = [UIImage imageNamed:@""];
    self.hd_report_one = @"";
}


- (IBAction)clickPicTwo:(id)sender {

    [self.view endEditing:YES];
    if ([self haveStr:@[self.hd_report_two]]) {
        [SQShowImage showImage:self.img_picTwo];
        
    }else{
        [LCImageChoose shareImageWithKey:@"" withImageMax:1 withCallBack:^(NSArray *data) {
            self.img_picTwo.image = data[0];
            self.hd_report_two = [UIImageJPEGRepresentation(self.img_picTwo.image, 0.5f) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
            
            self.img_picTwoDel.image = [UIImage imageNamed:@"del"];
        }];
    }
    
   
}

- (IBAction)clickDelPicTwo:(id)sender {
    self.img_picTwoDel.image = [UIImage imageNamed:@""];
    self.img_picTwo.image = [UIImage imageNamed:@"addImage"];
    self.hd_report_two = @"";
   
}


- (IBAction)clickPicThree:(id)sender {
  
    [self.view endEditing:YES];
    
    if ([self haveStr:@[self.hd_report_three]]) {
        [SQShowImage showImage:self.img_picThree];
        
    }else{
        [LCImageChoose shareImageWithKey:@"" withImageMax:1 withCallBack:^(NSArray *data) {
            self.img_picThree.image = data[0];
            self.hd_report_three = [UIImageJPEGRepresentation(self.img_picThree.image, 0.5f) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
            self.img_picThreeDel.image = [UIImage imageNamed:@"del"];
        }];
    }
    
}

- (IBAction)clickDelPicThree:(id)sender {
    self.img_picThreeDel.image = [UIImage imageNamed:@""];
   self.img_picThree.image = [UIImage imageNamed:@"addImage"];
    self.hd_report_three = @"";
}





@end
